package cn.yitun.chat.yitunserverimapicore.business.chat.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.base.chat.UserChatHistoryDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.userchat.UserChatContentDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.UserChatHistoryService;
import cn.yitun.chat.yitunserverimapicore.business.chat.service.UserChatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;

@Component
@ActionMapping(value = ActionTypeConstant.USER_CHAT_DATA_ACTION_PUSH)
public class UserChatDataAction {

	@Resource
	UserChatService userChatService;
	@Resource
	UserChatHistoryService userChatLogService;

	/**
	 * 用户聊天 计数
	 * Date 2019-05-22 17:57:32<br>
	 * @param socketSession
	 * @param query
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage queryCount(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {

		if (NumUtil.isInValid(query.getReceiveUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);
		ResultMessage rm = new ResultMessage();
		rm.bodyPut("count", userChatService.queryCount(query));
		return rm;
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * Description 用户聊天记录 列表
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage queryList(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (NumUtil.isInValid(query.getSendUserId()) || NumUtil.isInValid(query.getReceiveUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);
		ResultMessage rm = new ResultMessage();
		PageHelper.startPage(query.getCurrentPageNo(), query.getPageSize());
		rm.bodyPut("page", new PageInfo<>(userChatService.queryList(query)));
		return rm;
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * Description 获取详情
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0003")
	public ResultMessage getByContentId(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (NumUtil.isInValid(query.getId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return new ResultMessage(R.param("content", userChatService.getDetail(query.getId())));
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * Description 获取详情列表
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0004")
	public ResultMessage getByContentIds(SocketSession socketSession, @JsonDefine("data") UserChatHistoryDto query) {

		return new ResultMessage(R.param("list", YitunCollectionUtil.isEmpty(query.getIds()) ? new ArrayList<>() : userChatLogService.queryUserChatContentList(query)));
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 通过查询方向和点获取消息记录
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0007")
	public ResultMessage getListByStartId(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (StringUtil.isEmpty(query.getDirection()) || query.getSearchNum() == null || NumUtil.isInValid(query.getId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		return userChatLogService.getUserChatLogListByStartId(query);
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 通过查询方向和消息key获取消息记录
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0008")
	public ResultMessage getListByStartMessageKey(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (StringUtil.isEmpty(query.getDirection()) || NumUtil.isInValid(query.getSearchNum()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		if (NumUtil.isInValid(query.getMessageId()))
			query.setMessageId(null);
		return userChatLogService.getUserChatLogListByStartMessageKey(query);
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 更新发信息者的是否已读状态
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0009")
	public void updateToReadBySendUserId(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

        userChatLogService.updateUserOfflineMessageToRead(query.getReceiveUserId(), query.getSendUserId(), null, null);
	}
	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 更新发信息者的是否已读状态
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0011")
	public void updateToReadByContentId(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId())
                || NumUtil.isInValid(query.getId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

        userChatLogService.updateUserOfflineMessageToRead(query.getReceiveUserId(), query.getSendUserId(), query.getId(), null);
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 更新发信息者的是否已读状态 messageId
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0013")
	public void updateToReadByMessageKey(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId())
                || NumUtil.isInValid(query.getMessageId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

        userChatLogService.updateUserOfflineMessageToRead(query.getReceiveUserId(), query.getSendUserId(), null, query.getMessageId());
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 获取未读消息 计数
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0015")
	public ResultMessage queryUserChatUnreadCount(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
//		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId()))
//			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setIsRead(0);
		query.setReceiveUserId(socketSession.getKey());
		return userChatLogService.queryUserChatUnreadCount(query);
	}

	/**
	 * Date 2019-02-17 17:51:26<br>
	 * 获取未读消息 列表
	 * @param socketSession
	 * @param query
	 * @author yitun<br>
	 * @version 1.1
	 */
	@ActionMapping(value = "1.1.0016")
	public ResultMessage queryUserChatUnreadList(SocketSession socketSession, @JsonDefine("data") UserChatContentDto query) {
		if (NumUtil.isInValid(query.getReceiveUserId()) || NumUtil.isInValid(query.getSendUserId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setIsRead(0);
		return userChatLogService.queryUserChatUnreadList(query);
	}




}
